﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Web.Old_App_Code;

public partial class backstage_accredit : System.Web.UI.Page
{
    // Fields
    protected static string ac = "";
    protected string accreditXmlPath = "/xml/accredit.xml";
    protected DataEncryption de = new DataEncryption();

    protected static string mc = "";
    protected SoftReg sr = new SoftReg();

    // Methods
    protected void btnCreateMachineCode_Click(object sender, EventArgs e)
    {
        try
        {
            XmlDocument document = new XmlDocument();
            document.Load(base.Server.MapPath(this.accreditXmlPath));
            ((XmlElement)document.SelectSingleNode("accredit_set/machine_code")).SetAttribute("value", mc);
            this.txtMachineCode.Value = mc;
            document.Save(base.Server.MapPath(this.accreditXmlPath));
            base.Response.Redirect("accredit.aspx");
        }
        catch
        {
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {

            {
                XmlDocument document = new XmlDocument();
                document.Load(base.Server.MapPath(this.accreditXmlPath));
                ((XmlElement)document.SelectSingleNode("accredit_set/authorization_code")).SetAttribute("value", this.txtAuthorizationCode.Value);
                (document.SelectSingleNode("accredit_set/authorization_overdue_time") as XmlElement).SetAttribute("value", "");
                (document.SelectSingleNode("accredit_set/system_start_time") as XmlElement).SetAttribute("value", "");
                document.Save(base.Server.MapPath(this.accreditXmlPath));
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"恭喜您，授权成功！\");location.href=\"accredit.aspx\";top.topFrame.location=\"admin_top.aspx\";</script>");

            }
        }
        catch
        {
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
            }
            if (!base.IsPostBack)
            {
                this.txtMachineCode.Value = this.sr.getMNum();
                mc = this.sr.getMNum();
                ac = this.sr.getRNumByMNum(mc);
                XmlDocument document = new XmlDocument();
                document.Load(base.Server.MapPath(this.accreditXmlPath));
                XmlElement element = document.SelectSingleNode("accredit_set/machine_code") as XmlElement;
                this.txtMachineCode.Value = element.GetAttribute("value");
                XmlElement element2 = document.SelectSingleNode("accredit_set/authorization_code") as XmlElement;
                this.txtAuthorizationCode.Value = element2.GetAttribute("value");
                if ((element2.GetAttribute("value") == ac) && (element.GetAttribute("value") == mc))
                {
                    this.lblAccreditState.InnerText = "正版授权网站";
                    this.lblAccreditState.Style.Add("color", "#009900");
                }
                else
                {
                    XmlElement element3 = document.SelectSingleNode("accredit_set/authorization_overdue_time") as XmlElement;
                    XmlElement element4 = document.SelectSingleNode("accredit_set/system_start_time") as XmlElement;
                    if (element3.GetAttribute("value") != "")
                    {
                        DateTime time = Convert.ToDateTime(this.de.MD5Decrypt(element3.GetAttribute("value")));
                        DateTime now = DateTime.Now;
                        if (time > now)
                        {
                            TimeSpan span = (TimeSpan)(time - now);
                            int num = Math.Abs(span.Days);
                            this.lblAccreditState.InnerText = "非正版授权网站，剩余试用天数：" + num + "";
                        }
                        else
                        {
                            this.lblAccreditState.InnerText = "非正版授权网站，剩余试用天数：0";
                        }
                    }
                    else
                    {
                        element4.SetAttribute("value", this.de.MD5Encrypt(DateTime.Now.ToString()));
                        element3.SetAttribute("value", this.de.MD5Encrypt(DateTime.Now.AddDays(31.0).ToString()));
                        document.Save(base.Server.MapPath(this.accreditXmlPath));
                        this.lblAccreditState.InnerText = "非正版授权网站，剩余试用天数：30";
                    }
                }
            }
        }
        catch
        {
        }
    }
}
